<?php if(!empty($comments)) { ?>
<h5 class="mt-5">相关评论:</h5>
<?php } ?>
<!-- 设置data-root-id,data-root-type为评论取root_id,root值 -->
<div id="comment-list" data-root-id="<?=$root_id?>" data-root="<?=$root?>" data-status="<?=$check?>">
    <?php foreach($comments as $comment) { ?>
    <div class="card shadow-sm my-3 p-3 rounded-0" data-comment-id="<?=$comment['id']?>">
        <div class="row">
            <div class="col-auto">
                <img src="<?=$comment['user']['avatar']?>" class="rounded-circle" alt="Avatar" width="50" height="50">
            </div>
            <div class="col">
                <div class="d-flex">
                    <div class="flex-grow-1">
                        <small>
                            <a href="/app/laoha/user/index?user_id=<?=$comment['user_id']?>"
                                class="text-decoration-none text-primary-emphasis user-info" target="_blank">
                                <?=$comment['user']['nickname']?>
                            </a>
                        </small>
                        <p class="text-muted">
                            <small>
                                <?=timeTo($comment['created_at'])?>
                            </small>
                            <?php if($is_manage || $comment['user_id']==user_id()){ ?>
                            <a class="pull-right text-muted" data-type="delete-comment" href="javascript:;">
                                <small>删除</small>
                            </a>
                            <?php if($is_manage || ($comment['user_id']==user_id() && $comment['status']!==0)){  ?>
                            <a class="pull-right me-2 text-muted" data-type="edit-comment" href="javascript:;">
                                <small>编辑</small>
                            </a>
                            <?php } ?>
                            <?php } ?>
                        </p>
                    </div>
                </div>
                <div class="comment-content markdown-body">
                    <?php if($comment['status']!==0){  ?>
                    <p class="text-danger text-center" id="comment-check-<?=$comment['id']?>">此评论尚未通过审核
                        <?php if($is_manage){  ?>
                        ，<a href="javascript:;"
                            onclick="comment_check('/app/laoha/admin/comment/check?status=0&id=<?=$comment['id']?>','comment-check-<?=$comment['id']?>')">点此通过审核</a>
                        <?php } ?>
                    </p>
                    <?php } ?>
                    <div>
                        <?=$comment['html']?>
                    </div>
                </div>


                <div class="reply-list border p-3 rounded">
                    <?php 
                        if(!empty($comment['children'])){
                            foreach($comment['children'] as $k=>$r){ 
                        ?>
                    <div data-reply-id="<?=$r['id']?>">
                        <?php
                                if($k>0){
                                    echo '<hr/>';
                                }						
                            ?>
                        <div class="d-flex align-items-center">
                            <div class="flex-shrink-0">
                                <img src="<?=$r['user']['avatar']?>" class="rounded-circle" alt="Avatar" width="30"
                                    height="30">
                            </div>
                            <div class="flex-grow-1 ps-3">
                                <small>
                                    <a href="/app/laoha/user/index?user_id=<?=$comment['user_id']?>"
                                        class="text-decoration-none text-primary-emphasis user-info" target="_blank">
                                        <?=$r['user']['nickname']?>
                                    </a>
                                </small>
                                <small class="text-muted ms-3 fst-italic">
                                    <?=timeTo($r['created_at'])?>
                                </small>

                                <?php if($is_manage || $r['user_id']==user_id()){ ?>
                                <a class="text-muted pull-right" data-type="delete-reply" href="javascript:;">
                                    <small>删除</small>
                                </a>
                                <?php } ?>
                            </div>
                        </div>

                        <div class="reply-content markdown-body mt-1 ms-5">
                            <?php if($r['status']!==0){  ?>
                            <p class="text-danger text-center" id="reply-check-<?=$r['id']?>">此回复尚未通过审核
                                <?php if($is_manage){ ?>
                                ，<a href="javascript:;"
                                    onclick="comment_check('/app/laoha/admin/comment/check?status=0&id=<?=$r['id']?>','reply-check-<?=$r['id']?>')">点此通过审核</a>
                                <?php } ?>
                            </p>
                            <?php } ?>
                            <div class="fw-light">
                                <?=$r['html']?>
                            </div>
                        </div>
                    </div>
                    <?php 

                            }
                        }else{
                            echo '<div class="no-reply text-center text-secondary"><small>暂无回复</small></div>';
                        }
                        ?>
                </div>


                <?php if(user_id()){ ?>
                <div class="input-group my-2">
                    <textarea autoheight="true" class="form-control form-control-sm reply-textarea" rows="1"
                        placeholder="撰写回复 ..." name="content"></textarea>
                </div>
                <div class="mt-y d-flex align-content-center justify-content-end">
                    <button type="button" submit-type="reply" style="display: none" class="btn btn-primary btn-sm mt-1">
                        提交回复
                    </button>
                </div>
                <?php } ?>
            </div>
        </div>
    </div>
    <?php } ?>
</div>
<h5 class="mt-5">发表评论:</h5>
<?php if(user_id()){ ?>
<div>
    <div class="d-flex flex-sm-row align-items-center pt-2">
        <div id="editor">
            <textarea id="md-content"></textarea>
        </div>
    </div>

    <div class="fr mb1 justify-content-end d-flex">
        <button class="btn btn-primary" type="button" onclick="comment_submit()">
            发表评论
        </button>
    </div>
</div>


<div class="edit-comment modal" id="re_edit_box_outer" style="display: none;">
    <div class="modal-dialog modal-lg modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header" style="display:block">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span
                        class="sr-only">Close</span></button>
                <b class="modal-title">编辑评论</b>
            </div>

            <div class="modal-body" id="re_edit_box">
                <div id="re_editor">
                    <textarea style="display:none;" id="edit_content"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-large btn-success" onclick="edit_comment_save()">
                    确定
                </button>
            </div>
        </div>
    </div>
</div>
<?php }else{ ?>
<div class="card shadow-sm my-3 p-3 rounded-0">
    <p class="text-center mt-2">
        请先 <a href="/app/laoha/user/login" class="ajax-popup-link">登录</a>
    </p>
</div>
<?php } ?>
<link href="/app/laoha/vendor/editormd/css/editormd.min.css" rel="stylesheet">
<link href="/app/laoha/vendor/editormd/css/github-markdown.css" rel="stylesheet">
<script src="/app/laoha/js/prettify.js"></script>
<script src="/app/laoha/vendor/editormd/editormd.min.js"></script>
<script src="/app/laoha/js/comment.js"></script>